package whu.cn.whushare.ui.main.my.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.androidplus.util.ViewMapping;
import com.androidplus.util.ViewMappingUtil;

import java.util.List;

import whu.cn.whushare.R;
import whu.cn.whushare.bean.User;
import whu.cn.whushare.common.ImageWorkerManager;
import whu.cn.whushare.ui.main.known.bean.AllQuestion;
import whu.cn.whushare.ui.main.known.bean.Question;
import whu.cn.whushare.ui.main.my.bean.MyQuestion;
import whu.cn.whushare.util.Utils;

/**
 * Created by xiaoping on 2015/4/11.
 */
public class MyQuestionListAdapter extends BaseAdapter {
    private MyQuestion myQuestion; // 数据

    private Context context;

    /**
     *
     * @param context
     *            mainActivity
     * @param myQuestion
     *            显示的数据
     */
    public MyQuestionListAdapter(Context context, MyQuestion myQuestion) {
        this.context = context;
        this.myQuestion = myQuestion;

    }

    /*
     * 获得数据总数
     */
    @Override
    public int getCount() {
        return myQuestion.Question.size();
    }

    /*
     * 根据索引为position的数据
     */
    @Override
    public Object getItem(int position) {
        return myQuestion.Question.get(position);
    }

    /*
     * 根据索引值获得Item的Id
     */
    @Override
    public long getItemId(int position) {
        return position;
    }

    /*
     * 通过索引值position将数据映射到视图convertView具有缓存功能，在第一页时为null，在第二第三....页时不为null
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.layout_my_question_item, null);
            viewHolder = new ViewHolder();
            ViewMappingUtil.mapView(viewHolder, convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        Question question = myQuestion.Question.get(position);
        viewHolder.questionName.setText(question.getQuestionName() );
        viewHolder.answer_question_count.setText("回答:"+myQuestion.QuestionAnswerCounts.get(position)+ "");
        viewHolder.end_time.setText(question.getLastModify() + "");
        viewHolder.attentionNum.setText("关注："+myQuestion.QuestionAttentionCounts.get(position)+"");

        return convertView;
    }

    class ViewHolder {
        @ViewMapping(id = R.id.question)
        private TextView questionName;
        @ViewMapping(id = R.id.answer_count)
        private TextView answer_question_count;
        @ViewMapping(id = R.id.end_time)
        private TextView end_time;
        @ViewMapping(id=R.id.attention_num)
        private TextView attentionNum;
        @ViewMapping(id = R.id.my_question_layout)
        private RelativeLayout my_question_layout;
    }
}

